from constants import *
import pygame
from task import Task
from ds import DS

#####################################################
#                                                   #
#  CPU Class:                                       # 
#                                                   #
#  The CPU class is basically a QUEUE with two      #
#  differences:                                     #
#                                                   #
#  1. CPU is a execution unit                       #
#  2. CPU can only hold one task at a time          #
#                                                   #
#  Author: Felix Rohrer                             #     
#  Course: CS MET 575                               #
#  Year:   2011                                     #
#                                                   #
#####################################################

class CPU(DS):
  def __init__(self,name='CPU', pos=(50,50),drawAreaDim=(DRAWAREA_WIDTH,DRAWAREA_HEIGHT)):
    # call DS constructor and allow only one item
    DS.__init__(self,1, pos, name, drawAreaDim)
    self.DSType = 'QUEUE'

  # return task that will be removed next
  def peakNext(self):
    return self.tasks[0]

  # display CPU
  def display(self, canvas):
    # show actual CPU
    DS.display(self,canvas,bgColor=CPU_BG_COLOR)

    # draw CPU caption
    canvas.blit(self.textFont.render(self.name, True, (0,0,255)),(self.pos[0],self.pos[1]+self.size[0]+5))

  # CPU is execution unit, therefore tasks will execute while
  # being part of this CPU
  def isExecutionUnit(self):
    return True

  # push task to process on CPU
  # returns True if task successfully added, False otherwise
  def push(self,task):
    if self.tasks[0] is None or self.tasks[0]  == task:
      # add this task to the list
      task.setDS(self)
      self.tasks[0] = task
      # arrange task to be in the middle of the CPU field
      task.setPos((self.pos[0]+10,self.pos[1]+10))
      return True
    else:
      return False

  # get this data structures position
  def getPos(self):
    return (self.pos[0]+10, self.pos[1])

  # pop task from CPU
  def pop(self):
    self.tasks[0].setDS(None)
    self.tasks[0] = None

  # return true if no task in CPU
  def isEmpty(self):
    if self.tasks[0] is None:
      return True
    return False
